<?php
namespace app\admin\controller;

use think\facade\Cache;

use app\common\model\Category as CategoryModel;
class Login extends Base {

	public function sessionInfo() {
		if (session(config('app.USER_AUTH_KEY'))) {
			return responseData('success', '获取session成功', session(config('app.USER_AUTH_KEY')));
		} else {
			return responseData('fail', '用户尚未登录', 0);
		}
	}

	public function index() {
		$formData = input();
		$username = $formData["username"];

		/*if (!captcha_check($formData["captcha"])) {
			return responseData('fail','验证码错误');
		}*/
		$userData = db('user')->where('username', $username)->find();

		if (!$formData || pwdHash($formData["password"]) != $userData['password']) {
			return responseData('fail', '用户名或密码不正确');
		}

		if (!$userData['status']) {
			return responseData('fail', '用户已经被锁定');
		}

		$data['last_login_ip'] = request()->ip();
		$data['last_login_time'] = time();
		$loginData = db('user')->where('username', $username)->update($data);

		session(config('app.USER_AUTH_KEY'), $userData['id']);
		
		$this->setCache();

		return responseData('success', '登录成功', [
			'nickName' => $userData['nickname'],
			'id' => $userData['id'],
			'username' => $userData['username'],
		]);
	}

	public function logout() {
		if (session('?' . config('app.USER_AUTH_KEY'))) {
			session(null);
			return responseData('success', '登出成功!');
		} else {
			return responseData('success', '尚未登录，无需登出！');
		}
	}

}
